package com.server.questionbank.snframe.util.FileUtil.lifecycle.model;

import io.minio.messages.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
 * 生命周期规则模型
 */
public class LifecycleRule {
    private final Status status;
    private final String id;
    private final String prefix;
    private final Expiration expiration;
    private final Transition transition;
    private final NoncurrentVersionExpiration noncurrentVersionExpiration;
    private final NoncurrentVersionTransition noncurrentVersionTransition;
    private final AbortIncompleteMultipartUpload abortIncompleteMultipartUpload;

    private LifecycleRule(Builder builder) {
        this.status = builder.status;
        this.id = builder.id;
        this.prefix = builder.prefix;
        this.expiration = builder.expiration;
        this.transition = builder.transition;
        this.noncurrentVersionExpiration = builder.noncurrentVersionExpiration;
        this.noncurrentVersionTransition = builder.noncurrentVersionTransition;
        this.abortIncompleteMultipartUpload = builder.abortIncompleteMultipartUpload;
    }

    public io.minio.messages.LifecycleRule toMinioRule() {
        return new io.minio.messages.LifecycleRule(
            status,
            abortIncompleteMultipartUpload,
            expiration,
            new RuleFilter(prefix),
            id,
            noncurrentVersionExpiration,
            noncurrentVersionTransition,
            transition
        );
    }

    /**
     * 规则构建器
     */
    public static class Builder {
        private Status status = Status.ENABLED;
        private String id;
        private String prefix = "";
        private Expiration expiration;
        private Transition transition;
        private NoncurrentVersionExpiration noncurrentVersionExpiration;
        private NoncurrentVersionTransition noncurrentVersionTransition;
        private AbortIncompleteMultipartUpload abortIncompleteMultipartUpload;

        public Builder withId(String id) {
            this.id = id;
            return this;
        }

        public Builder withPrefix(String prefix) {
            this.prefix = prefix;
            return this;
        }

        public Builder withStatus(boolean enabled) {
            this.status = enabled ? Status.ENABLED : Status.DISABLED;
            return this;
        }

        // 过期策略 - 天数
        public Builder withExpirationDays(int days) {
            this.expiration = new Expiration((ZonedDateTime)null, days, null);
            return this;
        }

        // 过期策略 - 日期
        public Builder withExpirationDate(String dateStr) {
            this.expiration = new Expiration(parseDate(dateStr), null, null);
            return this;
        }

        public Builder withExpirationDate(ZonedDateTime date) {
            this.expiration = new Expiration(date, null, null);
            return this;
        }

        public Builder withTransitionDays(String storageClass, int days) {
            this.transition = new Transition((ZonedDateTime)null, days, storageClass);
            return this;
        }

        public Builder withTransitionDate(String storageClass, ZonedDateTime date) {
            this.transition = new Transition(date, null, storageClass);
            return this;
        }

        public Builder withNoncurrentVersionExpiration(int days) {
            this.noncurrentVersionExpiration = new NoncurrentVersionExpiration(days);
            return this;
        }

        public Builder withNoncurrentVersionTransition(String storageClass, int days) {
            this.noncurrentVersionTransition = new NoncurrentVersionTransition(days, storageClass);
            return this;
        }

        public Builder withAbortIncompleteMultipartUpload(int days) {
            this.abortIncompleteMultipartUpload = new AbortIncompleteMultipartUpload(days);
            return this;
        }

        private ZonedDateTime parseDate(String dateStr) {
            return ZonedDateTime.parse(dateStr + "T00:00:00Z", DateTimeFormatter.ISO_DATE_TIME);
        }

        public LifecycleRule build() {
            return new LifecycleRule(this);
        }
    }
} 